#include "mySort.h"
#include <iostream>
#include <string>

using namespace std;

MySort::MySort(){
    num = 0;
    origin = NULL;
}

MySort::MySort(int n, int* o){
    num = n;
    origin = new int[n];
    for(int i = 0; i < n; i ++){
        origin[i] = o[i];
    }
}

MySort::~MySort() = default;

void MySort::changeSwap(int& a, int& b){
    int temp = 0;
    temp = a;
    a = b;
    b = temp;
}

string MySort::printNum(){

}

string MySort::paritySort(){
    string str = "";
    for(int i = 1; i < num; i ++){
        int j = i % 2;
        for(j; j + 1 < num; j += 2){
            if(origin[j] > origin[j + 1]){
                changeSwap(origin[j], origin[j + 1]);
                for(int i = 0; i < num; i ++){
                str += to_string(origin[i]);
                if(i == num - 1){break;}
                str += " ";
            }
            str += "\n";
            }
        }
        
    }
    return str;
}

string MySort::bubbleSort(){
    string str;
    bool swap = 0;
    while(1){
        swap = 0;
        for(int i = 0; i < num - 1; i ++){
            if(origin[i] > origin[i + 1]){
                changeSwap(origin[i], origin[i + 1]);
                swap = 1;
                for(int i = 0; i < num; i ++){
                    str += to_string(origin[i]);
                    if(i == num - 1){break;}
                    str += " ";
                }
                str += "\n";
            }
        }
        if(swap == 0) break;
        swap = 0;
        for(int i = num - 1; i > 0; i --){
            if(origin[i] < origin[i - 1]){
                changeSwap(origin[i], origin[i - 1]);
                swap = 1;
                for(int i = 0; i < num; i ++){
                        str += to_string(origin[i]);
                        if(i == num - 1){break;}
                        str += " ";
                    }
                    str += "\n";
            }
        }
        if(swap == 0) break;
    }
    return str;
}
